package com.zjc.yohoshop.mvc.view;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import com.alipay.sdk.app.EnvUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.UploadHeadEntity;
import com.zjc.yohoshop.eventbus_bean.BaseEventBusEntity;
import com.zjc.yohoshop.eventbus_bean.UploadHeadBus;
import com.zjc.yohoshop.mvp.view.fragment.AddAddressFragment;
import com.zjc.yohoshop.mvp.view.fragment.AddressListFragment;
import com.zjc.yohoshop.mvp.view.fragment.AddressSelectorFragment;
import com.zjc.yohoshop.mvp.view.fragment.AffirmOrderFragment;
import com.zjc.yohoshop.mvp.view.fragment.CarListFragment;
import com.zjc.yohoshop.mvp.view.fragment.CollectionListFragment;
import com.zjc.yohoshop.mvp.view.fragment.CouponListFragment;
import com.zjc.yohoshop.mvp.view.fragment.CreateOrderFragment;
import com.zjc.yohoshop.mvp.view.fragment.GoodsValuesFragment;
import com.zjc.yohoshop.mvp.view.fragment.PayCenterFragment;
import com.zjc.yohoshop.mvp.view.fragment.SelUserFragment;
import com.zjc.yohoshop.mvp.view.fragment.SettingFragment;
import com.zjc.yohoshop.mvp.view.fragment.ShopDetailsFragment;
import com.zjc.yohoshop.mvp.view.fragment.UserLoginFragment;
import com.zjc.yohoshop.mvp.view.fragment.UserRegisterFragment;
import com.zjc.yohoshop.mvp.view.fragment.ZXingFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

/**
 * 用来显示其他Fragment的Activity
 */
public class FragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //启用沙箱环境测试
        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_fragment);

        initView();

    }

    private void initView() {
        Intent intent = getIntent();
        Fragment fragment = null;
        String type = intent.getStringExtra("type");
        if ("login".equals(type)){//登录页面
            fragment = new UserLoginFragment();
        }else if ("register".equals(type)){//注册页面
            fragment = new UserRegisterFragment();
        }else if ("zxing".equals(type)){//扫描二维码页面
            fragment = new ZXingFragment();
        }else if ("goods_values".equals(type)){//商品详情页面
            String goods_id = intent.getStringExtra("goods_id");
            fragment = new GoodsValuesFragment();
            Bundle bundle = new Bundle();
            bundle.putString("goods_id", goods_id);
            fragment.setArguments(bundle);
        }else if ("goods_car".equals(type)){//购物车页面
            fragment = new CarListFragment();
        }else if ("update_user".equals(type)){//个人信息页面
            fragment = new SelUserFragment();
        }else if ("create_order".equals(type)){//订单页面
            fragment = new CreateOrderFragment();
        }else if ("collection_list".equals(type)){//收藏商品列表界面
            fragment = new CollectionListFragment();
        }else if ("setting".equals(type)){//设置界面
            fragment = new SettingFragment();
        }else if ("addresslist".equals(type)){//收货地址列表界面
            fragment = new AddressListFragment();
        }else if ("addaddress".equals(type)){//添加收货地址界面
            fragment = new AddAddressFragment();
            Bundle bundle = new Bundle();
            bundle.putString("address_id", intent.getStringExtra("address_id"));
            fragment.setArguments(bundle);
        }else if ("coupon_list".equals(type)){//优惠券列表界面
            fragment = new CouponListFragment();
        }else if ("affirm_order".equals(type)){//确认订单界面
            fragment = new AffirmOrderFragment();
            fragment.setArguments(intent.getExtras());
        }else if ("address_selector".equals(type)){//其他地址界面
            fragment = new AddressSelectorFragment();
        } else if ("pay_center".equals(type)){//支付中心界面
            fragment = new PayCenterFragment();
            Bundle pay_center = new Bundle();
            pay_center.putDouble("sumPrice", intent.getDoubleExtra("sumPrice", 0));
            fragment.setArguments(intent.getExtras());
        }else if ("shop_details".equals(type)){//店铺详情界面
            fragment = new ShopDetailsFragment();
        }else{
            finish();
            return;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_fragment, fragment).commit();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (requestCode == BaseEventBusEntity.PHOTO && resultCode == Activity.RESULT_OK){
            EventBus.getDefault().post(new UploadHeadBus(BaseEventBusEntity.PHOTO, data));
        }else if (requestCode == BaseEventBusEntity.CAMERA && resultCode == Activity.RESULT_OK){
            EventBus.getDefault().post(new UploadHeadBus(BaseEventBusEntity.CAMERA, "成功"));
        }else {

        }

    }

}
